﻿using System;
using MosCraft.Utilities;

namespace MosCraft.Terrain.WorldGen
{
    public class WorldGenTreasureHut : WorldGenerator
    {
        public WorldGenTreasureHut()
        {
        }

        override public bool generate(World world, FastRandom rand, int x, int y, int z)
        {
            //Spawn only on top of grass
            int blockBellow = world.getBlockID(x, y, z);
            if (blockBellow != Block.Grass.blockID)
            {
                return false;
            }

            int hutWidth = x + 6;
            int hutHight = y + 4;
            int hutDepth = z + 6;
            int spireHight = 4;
            int hutHalfsize = 3;

            for (int deltaX = x; deltaX <= hutWidth; deltaX++)
            {
                for (int deltaY = y; deltaY <= hutHight; deltaY++)
                {
                    for (int deltaZ = z; deltaZ <= hutDepth; deltaZ++)
                    {
                        int blockID = 0;

                        if (deltaX == x || deltaX == hutWidth)
                        {
                            blockID = Block.Planks.blockID;
                        }

                        else if (deltaY == y || deltaY == hutHight)
                        {
                            if (deltaY == y)
                            {
                                blockID = Block.cobbleStone.blockID;
                            }
                            else
                            {
                                blockID = Block.Planks.blockID;
                            }
                        }

                        else if (deltaZ == z || deltaZ == hutDepth)
                        {
                            blockID = Block.Planks.blockID;
                        }

                        world.setBlockID(deltaX, deltaY, deltaZ, blockID);
                    }
                }
            }

            //Generate the 'Spire'
            int centreX = x + hutHalfsize;
            int centreZ = z + hutHalfsize;

            //This sets the hut orientation
            if (rand.nextInt(1) == 1)
            {
                world.setBlockID(x + hutHalfsize, y + 1, z, 0);
                world.setBlockID(x + hutHalfsize, y + 2, z, 0);
            }
            else
            {
                world.setBlockID(x, y + 1, z + hutHalfsize, 0);
                world.setBlockID(x, y + 2, z + hutHalfsize, 0);
            }
   
            //and the pointy spire
            for (int deltaY = hutHight; deltaY <= spireHight + hutHight; deltaY++)
            {
                world.setBlockID(centreX, deltaY, centreZ, Block.cobbleStone.blockID);
            }

            return true;
        }

        public int hieght { get; set; }
    }
}
